const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()

// 添加订单
exports.main = async (event, context) => {
  
  // 获取基础信息
  const wxContext = cloud.getWXContext()

  try {
      db.collection('orders').add({
        data: {
          // _id: event.data[i]._id
          arriveTime: new Date(),
          dateStr: "08-01 5:25",
          flyno: 'DL1254',
          dest: 'varsity + willow tree',
          box: '4个28寸行李箱，两个25寸行李箱，一个登机箱',  
          price: 123,  
          people: '3',
          driverId: null,   // 司机
          userId: wxContext.OPENID, // 下单用户的OpenId
        },
        success: function(res) {
          return {
            success: true,
            data: event.data
          }
        }
      })

  } catch (e) {
    return {
      success: false,
      errMsg: e
    }
  }
}